<?php
define('AJAX_SCRIPT', true);
require_once('../../config.php');
require_once($CFG->dirroot.'/local/myplugin/lib.php');


global $DB;
global $USER;
global $SESSION;
$tim = required_param('tim', PARAM_TEXT);
$keyword = required_param('keyword', PARAM_TEXT);
$program = required_param('program', PARAM_TEXT);
$major = required_param('major', PARAM_TEXT);

if($tim=='survey')
{
    //variable for temporary storing
    $aProgram = array();
    $majors = array();
    
    //init output html
    $str_program="";
			$str_program=$str_program.'<table class=generalbox border=1 align=center style="margin-left:16%;">'.
			'<th class="headingblock header">'.get_string('itusurvey','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('program','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('major','local_itusurveyplugin').'</th>'.
            //'<th class="headingblock header">'.get_string('create_date','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('last_update_date','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('expire_date','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('extend','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('edit','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('delete','local_itusurveyplugin').'</th>';
    
    $sql = "select * from mdl_itu_survey itus " 
    ."where itus.itus_title like '%$keyword%' ";
    
    //catch exception of first page load => get all survey instead
    if($program != 0){
        $sql = $sql."AND itus.id IN (select distinct sp_survey from mdl_surveyed_programs where sp_program = $program) ";
    }
    if($major != 0){
        $sql = $sql."AND itus.id IN (select distinct sm_survey from mdl_surveyed_majors where sm_major = $major) ";
    }
    
    //order desc by createdate->update date-> expire date
    $sql .= "order by itus.itus_create_date desc, itus.itus_update_date desc, itus.itus_expire_date asc";

    //echo $sql;
    $surveys = $DB->get_records_sql($sql);
    if(count($surveys) > 0){
        foreach($surveys as $survey){
                    
                    $aProgram = $DB->get_record("surveyed_programs", array('sp_survey'=>$survey->id));
                    $majors = $DB->get_records("surveyed_majors", array('sm_survey'=>$survey->id));
                    
                    $str_program=$str_program.'<tr style="cursor:pointer;"><td>'.$survey->itus_title.'</td>'
                    .'<td align=center>'.$aProgram->sp_code.'-'.$aProgram->sp_name.'</td>'
    			    .'<td align=center>';
                    
                    //loop to write majors
                    foreach($majors as $aMajor){
                        $str_program=$str_program.$aMajor->sm_code."-".$aMajor->sm_name."<br/>";
                    }
                    
                    $str_program=$str_program.'</td>';
                    //Dates: create, update, expire
                    //$create_date = date_create($survey->itus_create_date);
                    $update_date = date_create($survey->itus_update_date);
                    $expire_date = date_create($survey->itus_expire_date);
                    $curLang = $SESSION->lang;
                    if($curLang == "vi"){
                        $strUpdateDate = date_format($update_date,'d-F-Y H:i:s');
                        $strExpireDate = date_format($expire_date,'d-F-Y H:i:s');    
                    }else{
                        $strUpdateDate = date_format($update_date,'F-d-Y H:i:s');
                        $strExpireDate = date_format($expire_date,'F-d-Y H:i:s');
                    }   
                    //$str_program=$str_program.'<td>'.date_format($create_date,'d/M/Y H:i:s');
                    //$str_program=$str_program.'</td>';
                    $str_program=$str_program.'<td>'.$strUpdateDate;
                    $str_program=$str_program.'</td>';
                    $str_program=$str_program.'<td>'.$strExpireDate;
                    $str_program=$str_program.'</td>';
                    //Extend button
                    $str_program=$str_program.'<td>';
                        $str_program=$str_program.'<input type="text" name="extend'.$survey->id.'" id="extend'.$survey->id.'" value="0" readonly="readonly" size="5"/><br/>';
                        $str_program=$str_program.'<img width="16px" height="16px" src="../css/images/plus-icon.png" onclick="controlDigitNumberInDiv(\'extend'.$survey->id.'\',\'+\',1);"/></>&nbsp;';
                        $str_program=$str_program.'<input type="button" name="extBtn" id="extBtn" onclick="extendSurvey(\''.$survey->id.'\',\''.$USER->sesskey.'\');" value="<->"/>';    
                        $str_program=$str_program.'&nbsp;<img width="16px" height="16px" src="../css/images/minus-icon.png" onclick="controlDigitNumberInDiv(\'extend'.$survey->id.'\',\'-\',1);"/></>';    
                    $str_program=$str_program.'</td>';
                    //check edit btn display: only not publish survey can be edited
                    if($survey->itus_publish_date == ""){
                        //only edit own survey
                        if($USER->id == $survey->itus_creator){
                            $str_program=$str_program.'<td align=center "><a href="edit_survey.php?id='.$survey->id.'"><img src="../css/images/edit.jpg" alt="Edit" /></a></td>';
                        }                            
                        else{
                            $str_program=$str_program.'<td align=center"></td>';    
                        }                                
                    }else{
                        $str_program=$str_program.'<td align=center"></td>';
                    }
                    //only delete own survey
                    if($USER->id == $survey->itus_creator){
                        $str_program=$str_program.'<td align=center onClick="delete_survey(\''.get_string('deleteSurveyNotification','local_itusurveyplugin').'\',\''.$survey->itus_title.'\',\''.$survey->id.'\',\''.$USER->sesskey.'\');"><img src="../css/images/delete.png" alt="Delete" /></td>';
                    }                            
                    else{
                        $str_program=$str_program.'<td align=center"></td>';    
                    } 
                    
                    $str_program=$str_program.'</tr>';
        }
        $str_program=$str_program.'</table>';
    }else{
        $str_program=$str_program.'<tr><td colspan="8">'.get_string('notfound','local_itusurveyplugin').'</td></tr>';
        $str_program=$str_program.'</table>';
    }
	echo $str_program;   
}
else if($tim=='surveyTeacher'){
    //init output html
    $new="";
			$new=$new.'<table class=generalbox border=1 align=center style="margin-left:16%; margin-right:16%; width:80%;">'.
            '<tr class="headingblock header"><td colspan="5">'.get_string('newSurvey','local_itusurveyplugin').'</td></tr>'.
			'<th class="headingblock header">'.get_string('itusurvey','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('program','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('major','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('subject','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('expire_date','local_itusurveyplugin').'</th>';
    $cur="";
			$cur=$cur.'<table class=generalbox border=1 align=center style="margin-left:16%; margin-right:16%; width:80%;">'.
            '<tr class="headingblock header"><td colspan="5">'.get_string('ongoingSurvey','local_itusurveyplugin').'</td></tr>'.
			'<th class="headingblock header">'.get_string('itusurvey','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('program','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('major','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('subject','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('expire_date','local_itusurveyplugin').'</th>';
            
   $submited="";
			$submited=$submited.'<table class=generalbox border=1 align=center style="margin-left:16%; margin-right:16%; width:80%;">'.
            '<tr class="headingblock header"><td colspan="5">'.get_string('submmitedSurvey','local_itusurveyplugin').'</td></tr>'.
			'<th class="headingblock header">'.get_string('itusurvey','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('program','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('major','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('subject','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('expire_date','local_itusurveyplugin').'</th>';
    
            
    
    $sql = "select its.id, sts_subject_id, sts_survey_id, sts_teacher_id, sts_status, itus.itus_publish_date, itus.itus_expire_date ";
    $sql .= "from mdl_itus_teachers_subjects its, mdl_itu_survey itus ";
    $sql .= "where sts_teacher_id = ".$keyword." and itus.id = its.sts_survey_id and itus.itus_publish_date is not NULL order by sts_status asc, itus.itus_expire_date asc";
    //echo $sql;
    $surveys = $DB->get_records_sql($sql);
    if(count($surveys) > 0){
        $subject = array();
        $surveyData = array();
        $aProgram = array();
        $majors = array();
        $curLang = $SESSION->lang;
        
        foreach($surveys as $survey){
            $subject = $DB->get_record("surveyed_subjects", array('ss_survey'=>$survey->sts_survey_id, 'ss_subject'=>$survey->sts_subject_id));
            $surveyData = $DB->get_record("itu_survey", array('id'=>$survey->sts_survey_id));
            $aProgram = $DB->get_record("surveyed_programs", array('sp_survey'=>$survey->sts_survey_id));
            $majors = $DB->get_records("surveyed_majors", array('sm_survey'=>$survey->sts_survey_id));
            
            if($survey->sts_status == 0){
                //prgram
                $nowInt = time();
                $expireDateInt = strtotime($survey->itus_expire_date);
                if($expireDateInt > $nowInt){
                    $new .= '<tr style="cursor:pointer;"><td><a href="./dosurvey.php?itus='.$survey->sts_survey_id.'&sid='.$survey->sts_subject_id.'">'.$surveyData->itus_title.'</a></td><td>'.$aProgram->sp_name.'</td>';    
                }else{
                    $new .= '<tr style="cursor:pointer;"><td>'.$surveyData->itus_title.'</td><td>'.$aProgram->sp_name.'</td>';
                }
                //majors
                $new .= '<td>';
                foreach($majors as $major){
                    $new .= $major->sm_name."<br/>";
                }
                $new .= '</td>';
                //subject
                $new .= '<td>';
                $new .= $subject->ss_name_vn."-".$subject->ss_name_en;
                $new .= '</td>';
                //expire date
                    $expire_date = date_create($surveyData->itus_expire_date);
                    if($curLang == "vi"){
                        $strExpireDate = date_format($expire_date,'d-F-Y H:i:s');    
                    }else{
                        $strExpireDate = date_format($expire_date,'F-d-Y H:i:s');
                    }
                $new .= '<td>'.$strExpireDate.'</td>';
            }
            
            if($survey->sts_status == 1){
                //prgram
                //retake_survey()
                $nowInt = time();
                $expireDateInt = strtotime($survey->itus_expire_date);
                if($expireDateInt > $nowInt){
                    $cur .= '<tr style="cursor:pointer;"><td><a href="./dosurvey.php?itus='.$survey->sts_survey_id.'&sid='.$survey->sts_subject_id.'">'.$surveyData->itus_title.'</a></td><td>'.$aProgram->sp_name.'</td>';    
                }else{
                    $cur .= '<tr style="cursor:pointer;"><td>'.$surveyData->itus_title.'</td><td>'.$aProgram->sp_name.'</td>';
                }
                //$cur .= '<tr style="cursor:pointer;"><td><a href="./dosurvey.php?itus='.$survey->sts_survey_id.'&sid='.$survey->sts_subject_id.'">'.$surveyData->itus_title.'</a></td><td>'.$aProgram->sp_name.'</td>';
                //majors
                $cur .= '<td>';
                foreach($majors as $major){
                    $cur .= $major->sm_name."<br/>";
                }
                $cur .= '</td>';
                //subject
                $cur .= '<td>';
                $cur .= $subject->ss_name_vn."-".$subject->ss_name_en;
                $cur .= '</td>';
                //expire date
                    $expire_date = date_create($surveyData->itus_expire_date);
                    if($curLang == "vi"){
                        $strExpireDate = date_format($expire_date,'d-F-Y H:i:s');    
                    }else{
                        $strExpireDate = date_format($expire_date,'F-d-Y H:i:s');
                    }
                $cur .= '<td>'.$strExpireDate.'</td>';
            }
            
            if($survey->sts_status == 2){
                //prgram
                $nowInt = time();
                $expireDateInt = strtotime($survey->itus_expire_date);
                if($expireDateInt > $nowInt){
                    $submited .= '<tr style="cursor:pointer;"><td>'.$surveyData->itus_title.'(<a href="./dosurvey.php?itus='.$survey->sts_survey_id.'&sid='.$survey->sts_subject_id.'">Retake</a>)</td><td>'.$aProgram->sp_name.'</td>';    
                }else{
                    $submited .= '<tr style="cursor:pointer;"><td>'.$surveyData->itus_title.'</td><td>'.$aProgram->sp_name.'</td>';
                }                
                //majors
                $submited .= '<td>';
                foreach($majors as $major){
                    $submited .= $major->sm_name."<br/>";
                }
                $submited .= '</td>';
                //subject
                $submited .= '<td>';
                $submited .= $subject->ss_name_vn."-".$subject->ss_name_en;
                $submited .= '</td>';
                //expire date
                    $expire_date = date_create($surveyData->itus_expire_date);
                    if($curLang == "vi"){
                        $strExpireDate = date_format($expire_date,'d-F-Y H:i:s');    
                    }else{
                        $strExpireDate = date_format($expire_date,'F-d-Y H:i:s');
                    }
                $submited .= '<td>'.$strExpireDate.'</td>';
            }
            
        }
        $new .= '</table>';
        $cur .= '</table>';
        $submited .= '</table>';
    }
    echo $new;
    echo $cur;
    echo $submited;
}
else if($tim=='filter_survey'){
    //variable for temporary storing
    $aProgram = array();
    $majors = array();
    
    //init output html
    $str_program="";
			$str_program=$str_program.'<table class=generalbox border=1 align=center style="margin-left:16%;">'.
			'<th class="headingblock header">'.get_string('itusurvey','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('program','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('major','local_itusurveyplugin').'</th>'.
            //'<th class="headingblock header">'.get_string('create_date','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('last_update_date','local_itusurveyplugin').'</th>'.
            '<th class="headingblock header">'.get_string('expire_date','local_itusurveyplugin').'</th>'.
            //'<th class="headingblock header">'.get_string('extend','local_itusurveyplugin').'</th>'.
			//'<th class="headingblock header">'.get_string('edit','local_itusurveyplugin').'</th>'.
			'<th class="headingblock header">'.get_string('clone','local_itusurveyplugin').'</th>';
    
    $sql = "select * from mdl_itu_survey itus " 
    ."where itus.itus_title like '%$keyword%' ";
    
    //catch exception of first page load => get all survey instead
    if($program != 0){
        $sql = $sql."AND itus.id IN (select distinct sp_survey from mdl_surveyed_programs where sp_program = $program) ";
    }
    if($major != 0){
        $sql = $sql."AND itus.id IN (select distinct sm_survey from mdl_surveyed_majors where sm_major = $major) ";
    }
    
    //order desc by createdate->update date-> expire date
    $sql .= "order by itus.itus_create_date desc, itus.itus_update_date desc, itus.itus_expire_date asc";

    //echo $sql;
    $surveys = $DB->get_records_sql($sql);
    if(count($surveys) > 0){
        foreach($surveys as $survey){
                    
                    $aProgram = $DB->get_record("surveyed_programs", array('sp_survey'=>$survey->id));
                    $majors = $DB->get_records("surveyed_majors", array('sm_survey'=>$survey->id));
                    
                    $str_program=$str_program.'<tr style="cursor:pointer;"><td>'.$survey->itus_title.'</td>'
                    .'<td align=center>'.$aProgram->sp_code.'-'.$aProgram->sp_name.'</td>'
    			    .'<td align=center>';
                    
                    //loop to write majors
                    foreach($majors as $aMajor){
                        $str_program=$str_program.$aMajor->sm_code."-".$aMajor->sm_name."<br/>";
                    }
                    
                    $str_program=$str_program.'</td>';
                    //Dates: create, update, expire
                    //$create_date = date_create($survey->itus_create_date);
                    $update_date = date_create($survey->itus_update_date);
                    $expire_date = date_create($survey->itus_expire_date);
                    $curLang = $SESSION->lang;
                    if($curLang == "vi"){
                        $strUpdateDate = date_format($update_date,'d-F-Y H:i:s');
                        $strExpireDate = date_format($expire_date,'d-F-Y H:i:s');    
                    }else{
                        $strUpdateDate = date_format($update_date,'F-d-Y H:i:s');
                        $strExpireDate = date_format($expire_date,'F-d-Y H:i:s');
                    }   
                    //$str_program=$str_program.'<td>'.date_format($create_date,'d/M/Y H:i:s');
                    //$str_program=$str_program.'</td>';
                    $str_program=$str_program.'<td>'.$strUpdateDate;
                    $str_program=$str_program.'</td>';
                    $str_program=$str_program.'<td>'.$strExpireDate;
                    $str_program=$str_program.'</td>';
                    //Clone btn
                    $str_program=$str_program.'<td align=center onClick="clone_survey(\''.$survey->id.'\');"><img src="../css/images/clone-icon.gif" alt="Clone" /></td>'
                    .'</tr>';
        }
        $str_program=$str_program.'</table>';
    }else{
        $str_program=$str_program.'<tr><td colspan="6">'.get_string('notfound','local_itusurveyplugin').'</td></tr>';
        $str_program=$str_program.'</table>';
    }
	echo $str_program;
}
else if($tim=='majorbyprogram'){
    $sql = "select * from mdl_major where program_id = $program";
    $outputs = $DB->get_records_sql($sql);
    echo json_encode($outputs);    
}
else if($tim=='maxLevOfProgram'){
    $sql = "select MAX(DISTINCT  lo_level) as max_level from mdl_learning_outcome where program_id = $program";
    $output = $DB->get_record_sql($sql);
    echo $output->max_level;    
}


?>